//
//  ExhibitionListViewController.m
//  homeland
//
//  Created by SunShine on 14-1-18.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "ExhibitionListViewController.h"
#import "ExhibitionMonthListViewController.h"
#import "ExhibitionDetailViewController.h"
#import "ExhibitionService.h"

@interface ExhibitionListViewController ()

@property (strong, nonatomic) IBOutlet UILabel *tipsLabel;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) NSInteger currentMonth;
@property (strong, nonatomic) NSArray *dataList;
@end

@implementation ExhibitionListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"展会排期";
    self.navigationItem.rightBarButtonItem = rightNavBtnWithImgName(@"选择月份", self, @selector(changeMonthButtonAction:));
    self.tipsLabel.text = @"数据来源于广州琶洲会展经济促进会";
    self.currentMonth = 0;
    [self loadList];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Data Method
-(void)loadList
{
    [self startLoadingView];
    NSString *month;
    if (self.currentMonth < 9) {
        month = [NSString stringWithFormat:@"20140%d", self.currentMonth+1];
    } else {
        month = [NSString stringWithFormat:@"2013%d", self.currentMonth+1];
    }
    [[ExhibitionService new] getExhibitionListForMonth:month Success:^(NSArray *data) {
        self.dataList = data;
        [self.tableView reloadData];
        [self stopLoadingView];
    } Failure:^(NSString *msg) {
        [self showAlertWithTitle:@"错误" Msg:msg];
        [self stopLoadingView];
    }];
}


#pragma mark - Button
-(void)changeMonthButtonAction:(id)sender
{
    ExhibitionMonthListViewController *monthVC = [[ExhibitionMonthListViewController alloc] initWithNibName:@"ExhibitionMonthListViewController" bundle:nil];
    monthVC.currentIndex = self.currentMonth;
    monthVC.monthChangedBlock = ^(NSInteger index) {
        self.currentMonth = index;
        [self loadList];
    };
    [self.navigationController pushViewController:monthVC animated:YES];
}

#pragma mark - Table Method
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (self.dataList?self.dataList.count:0);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"exhibitionListCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.backgroundColor = [UIColor clearColor];
    //数据
    NSDictionary *dic = [self.dataList objectAtIndex:indexPath.row];
    NSString *time = [dic objectForKey:@"time"];
    NSString *title = [dic objectForKey:@"title"];
    cell.textLabel.text = [NSString stringWithFormat:@"%@%@", time, title];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ExhibitionDetailViewController *detailVC = [[ExhibitionDetailViewController alloc] initWithNibName:@"ExhibitionDetailViewController" bundle:nil];
    detailVC.infoItem = [self.dataList objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}

//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    NSString *title = [NSString stringWithFormat:@"%d月", self.currentMonth+1];
//    return title;
//}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 24.0)];
    NSString *title = [NSString stringWithFormat:@"%d月", self.currentMonth+1];
    label.text = title;
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor lightGrayColor];
    return label;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 24.0;
}
@end
